<template>
  <div ref="wrapper" class="list-wrapper">
    <div class="scroll-content">
      <div ref="listWrapper">
        <slot name="content">
        </slot>
        <slot name="list" >
          <div v-if="isShow === 1" style="position: relative;top: -35px;text-align: center;background-color: #fff;border-radius: 20px 20px 0px 0px;border-top: 1px solid #ccc;box-shadow: 0px -2px 9px #888888;z-index: 100;pading-bottom:35px;">
            <div class="list-header">
              <div class="list_left">
                <span style="color:#278672;font-weight:900;">
                  /
                </span>
                <span style="color:#000;">
                  为您推荐
                </span>
              </div>
            </div>
            
            <!-- <ul class="list-content">
              <li  class="list-item" v-for="(item,index) in data" :key="index">{{item}}</li>
            </ul>-->
            <s-list :list="data"></s-list> 
          </div>
          
        </slot>
      </div>
      <slot name="pullup"
            :pullUpLoad="pullUpLoad"
            :isPullUpLoad="isPullUpLoad"
      >
        <div class="pullup-wrapper" v-if="pullUpLoad" :style="pos">
          <div class="before-trigger" v-if="!isPullUpLoad">
            <!-- <span>{{pullUpTxt}}</span> -->
          </div>
          <div class="after-trigger" v-else>
            <spinner></spinner>
          </div>
        </div>
      </slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  import {Spinner} from 'vux' 
  import { getRect } from '@/helps/index'
  import SList from 'components/shangjia-list'
  const COMPONENT_NAME = 'scroll'

  export default {
    name: COMPONENT_NAME,
    props: {
      data: {
        type: Array,
        default: function () {
          return []
        }
      },
      isShow:{
        type: Number,
        default: 0
      },
      pos:{
        type: String,
        default: 'top:-50px'
      },
      probeType: {
        type: Number,
        default: 1
      },
      click: {
        type: Boolean,
        default: true
      },
      listenScroll: {
        type: Boolean,
        default: false
      },
      listenBeforeScroll: {
        type: Boolean,
        default: false
      },
      scrollbar: {
        type: null,
        default: false
      },
      pullUpLoad: {
        type: null,
        default: false
      },
      startY: {
        type: Number,
        default: 0
      },
      refreshDelay: {
        type: Number,
        default: 20
      },
      freeScroll: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        isRebounding: false,
        isPullUpLoad: false,
        pullUpDirty: true
      }
    },
    computed: {
      pullUpTxt() {
        const moreTxt = this.pullUpLoad && this.pullUpLoad.txt && this.pullUpLoad.txt.more

        const noMoreTxt = this.pullUpLoad && this.pullUpLoad.txt && this.pullUpLoad.txt.noMore

        return this.pullUpDirty ? moreTxt : noMoreTxt
      }
    },
    mounted() {
      setTimeout(() => {
        this.initScroll()
      }, 20)
    },
    methods: {
      initScroll() {
        if (!this.$refs.wrapper) {
          return
        }
        if (this.$refs.listWrapper && (this.pullUpLoad)) {
          this.$refs.listWrapper.style.minHeight = `${getRect(this.$refs.wrapper).height + 1}px`
        }

        let options = {
          probeType: this.probeType,
          click: this.click,
          scrollY: true,
          scrollX: false,
          scrollbar: this.scrollbar,
          pullUpLoad: this.pullUpLoad,
          startY: this.startY,
          freeScroll: this.freeScroll
        }

        this.scroll = new BScroll(this.$refs.wrapper, options)

        // if (this.listenScroll) {
        //   this.scroll.on('scroll', (pos) => {
        //     this.$emit('scroll', pos)
        //   })
        // }

        if (this.listenBeforeScroll) {
          this.scroll.on('beforeScrollStart', () => {
            this.$emit('beforeScrollStart')
          })
        }
        if (this.pullUpLoad) {
          this._initPullUpLoad()
        }
      },
      disable() {
        this.scroll && this.scroll.disable()
      },
      enable() {
        this.scroll && this.scroll.enable()
      },
      refresh() {
        this.scroll && this.scroll.refresh()
      },
      scrollTo() {
        this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
      },
      scrollToElement() {
        this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
      },
      destroy() {
        this.scroll.destroy()
      },
      forceUpdate(dirty) {
       if (this.pullUpLoad && this.isPullUpLoad) {
          this.isPullUpLoad = false
          this.scroll.finishPullUp()
          this.pullUpDirty = dirty
          this.refresh()
        } else {
          this.refresh()
        }
      },
      _initPullUpLoad() {
        this.scroll.on('pullingUp', () => {
          this.isPullUpLoad = true
          this.$emit('pullingUp')
        })
      }
    },
    watch: {
      data() {
        setTimeout(() => {
          this.forceUpdate(true)
        }, this.refreshDelay)
      }
    },
    components: {
      Spinner,
      SList
    }
  }

</script>

<style lang="css" scoped>
  
.list-wrapper {
  position: absolute;
  left: 0;
  top:0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}
.list-wrapper .scroll-content {
  position: relative;
  z-index: 1;
}
.list-header{
	display: flex;
	flex-direction: row;
	-webkit-flex-direction: row;
	justify-content:space-between;
	-webkit-justify-content:space-between;
	align-items: center;
	-webkit-align-items: center;
	flex-wrap: nowrap;
	-webkit-flex-wrap: nowrap;
	padding: .46rem;
  border-bottom: 1px solid #ccc;
}
span{
	font-size: 16px;
	color: #2b2b2b;
}
.list-wrapper .list-content {
  position: relative;
  z-index: 10;
}
.list-wrapper .list-content .list-item {

  font-size: 18px;
  padding-left: 20px;
  border-bottom: 1px solid #e5e5e5;
}
.pullup-wrapper {
  width: 100%;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  padding: 16px 0;
  position: relative;
  top: -50px;
}

</style>
